﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace orz.mouxianyu.Extensions.ToolClass
{
    /// <summary>
    /// 返回json包装工具
    /// </summary>
    public class ApiResponse
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; }

        /// <summary>
        /// 返回提示
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string Message { get; }

        public ApiResponse(int statusCode, string message = null)
        {
            StatusCode = statusCode;
            Message = message ?? GetDefaultMessageForStatusCode(statusCode);
        }

        private static string GetDefaultMessageForStatusCode(int statusCode)
        {
            switch (statusCode)
            {
                //...
                case 404:
                    return "Resource not found";
                case 500:
                    return "An unhandled error occurred";
                default:
                    return null;
            }
        }


    }

    /// <summary>
    /// 返回模型
    /// </summary>
    public class ApiOkResponse : ApiResponse
    {
        /// <summary>
        /// 包装起来的返回json（对象）
        /// </summary>
        public object Result { get; }

        public ApiOkResponse(object result)
            : base(200, "Success")
        {
            Result = result;
        }
        public ApiOkResponse(object result,string message)
            : base(200, message)
        {
            Result = result;
        }
    }
}
